import React from 'react';
import { observer, inject } from 'mobx-react';
import { withRouter } from 'react-router-dom';
import { Icon,ButtonSelect } from 'weaver-mobile';
import { Browser } from 'weaver-mobile-page';
import * as API_REQ from '../../apis/form';
import * as Util from '../../util/formUtil';
import { _, DetailUtil, FieldUtil } from '../../util/pcPublic';
import { getLabel } from '../../util/common';

@inject('wfGlobalStore','wfLayoutStore')
@withRouter
@observer
export default class DetailButton extends React.Component{
    constructor(props){
        super(props);
    }
    doSapBrowserChange(_ids, _names, _datas, rowDatas){
        const { symbol,wfGlobalStore,wfLayoutStore } = this.props;
        const { requestid,workflowid } = wfGlobalStore.commonParam;
        const { detailMap } = wfLayoutStore;
        const detailObj = detailMap.get(symbol) || {};
        const { fielddbtype } = detailObj.attr.sapProps;
        let sapParams = { fromNodeorReport:"0",requestid,workflowid,fielddbtype };
        sapParams["datas"] = JSON.stringify(rowDatas);
        API_REQ.convertSapDatas(sapParams).then(result =>{
            result.datas && result.datas.map(addRowData =>{
                DetailUtil.addRow(symbol, addRowData, false);
            });
        });
    }
    render(){
        const { symbol,wfGlobalStore,wfLayoutStore } = this.props;
        const { commonParam } = wfGlobalStore;
        const { requestid,workflowid } = commonParam;
        const canEdit = parseInt(commonParam.isviewonly || 0) !== 1 && parseInt(commonParam.isaffirmance || 0) !== 1;
        if(!canEdit)
            return <div className="detailButtonDiv"></div>
        const { detailMap,mainData } = wfLayoutStore;
        const detailObj = detailMap.get(symbol) || {};
        const checkedRowArr = DetailUtil.getDetailCheckedRowArr(detailObj);
        const disabledClassName = checkedRowArr.length > 0 ? "" : "detailBtnDisabled";
        const { attr,dropBtnKey } = detailObj;
        const { isadd,iscopy,isdelete,isopensapmul,sapProps } = attr;
        const isCardEdit = DetailUtil.judgeIsCardEdit(detailObj);
        const showSap = isopensapmul === 1 && !_.isEmpty(sapProps);
        const detailIndex = parseInt(symbol.substring(7)) - 1;
        const addBtn = (
            <Icon key="addBtn" type="tianjia" onClick={()=>{Util.addRow(symbol, isCardEdit)}} className={`wf-dbtn-add detailBtn`} />
        );
        const delBtn = (
            <Icon key="delBtn" type="shanchu" onClick={()=>{Util.delRow(symbol)}} className={`wf-dbtn-delete detailBtn ${disabledClassName}`} />
        );
        const copyBtn = (
            <Icon key="copyBtn" type="replication" onClick={()=>{DetailUtil.copyDetailRow(symbol)}} className={`wf-dbtn-copy detailBtn ${disabledClassName}`} />
        );

        let btns = [];
        let sapDom = null;
        if(showSap){
            let dataParams = { requestid,workflowid };
            const { fielddbtype,relateFields } = sapProps;
            dataParams["fielddbtype"] = fielddbtype;
            relateFields && relateFields.map(fid =>{
                const fieldKey = `field${fid}`;
                const fieldValue = mainData.has(fieldKey) ? mainData.get(fieldKey).value : "";
                dataParams[fieldKey] = fieldValue;
            });
            if(relateFields)
                dataParams = {...dataParams, ...FieldUtil.getRelateFieldShowName(relateFields, -1)};
            const { sapBtnStore } = detailObj;
            const sapBrowProps = {
                id:"fieldSapMulti", type:"227", title:getLabel(30313,"系统集成多选浏览按钮"),
                value:"", valueArray:[], isSingle:false, isMultCheckbox:false, hasAdvanceSerach:true, sap:true,
                dataParams:dataParams, completeParams:dataParams, conditionDataParams:dataParams,
                store: sapBtnStore,
                onChange: this.doSapBrowserChange.bind(this)
            };
            sapDom = <Browser {...sapBrowProps}><span /></Browser>;
            btns.push(<Icon key="sapBtn" type="replication" className={`wf-dbtn-sap detailBtn`} onClick={()=>{
                this.props.history.push(sapBtnStore.path);
            }} />);
        }
        if(isadd === 1){
            btns.push(addBtn);
            if(iscopy === 1 && isdelete !== 1){
                const buttonGroup = [
                    {
                        value:"1",
                        label:getLabel(91,"删除"),
                        buttonProps: {type:"shanchu", className:`wf-dbtn-delete detailBtn ${disabledClassName}`, onClick: ()=>{Util.delRow(symbol)}}
                    },
                    {
                        value:"2",
                        label:getLabel(77,"复制"),
                        buttonProps: {type:"replication", className:`wf-dbtn-copy detailBtn ${disabledClassName}`, onClick: ()=>{DetailUtil.copyDetailRow(symbol)}}
                    }
                ];
                btns.push(<ButtonSelect
                    value={dropBtnKey.toString()}
                    buttonGroup={buttonGroup}
                    onChange={key => {
                        detailObj.setDropBtnKey(parseInt(key[0]));
                    }}
                />);
            }else{
                btns.push(delBtn);
                iscopy === 1 && btns.push(copyBtn);
            }
        }else if(isdelete === 1){
            btns.push(delBtn);
        }
        return (
            <div className="detailButtonDiv">
                { btns }
                { sapDom }
            </div>
        );
    }
}